package web.servlet;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import utils.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@WebServlet("/upload")
public class FileUploadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            //创建磁盘文件工厂对象
            DiskFileItemFactory factory = new DiskFileItemFactory();

            //创建文件上传你核心类
            ServletFileUpload upload = new ServletFileUpload(factory);
            //设置上传文件的编码
            upload.setHeaderEncoding("utf-8");
            //判断表单是否为文件上传
            boolean multipartContent = upload.isMultipartContent(req);

            if (multipartContent){
                //解析reque  获取表单项的集合
                List<FileItem> list = upload.parseRequest(req);
                if(list != null){
                    //遍历集合 获取表单项
                    for (FileItem fileItem : list) {
                        //判断当前表单项是否为普通表单项
                        boolean formField = fileItem.isFormField();
                        if (formField){
                            //普通表单项
                            String fieldName = fileItem.getFieldName();
                            String fieldValue = fileItem.getString("utf-8");
                            System.out.println(fieldName+"="+fieldValue);
                        }else {
                            //文件上传项
                            //获取文件名
                            String fieldName = fileItem.getName();
                            //拼接新的文件名 使用UUID保证不重复
                            String newFileName = UUIDUtils.getUUID() + "_" + fieldName;

                            //获取输入流
                            InputStream inputStream = fileItem.getInputStream();

                            //创建输出流
                            //获取项目的运行目录
                            String realPath = this.getServletContext().getRealPath("/");
                            //截取路径
                            String webappsPath = realPath.substring(0, realPath.indexOf("edu_home"));
                            //拼接路径
                            FileOutputStream fileOutputStream = new FileOutputStream(webappsPath+"/upload/"+newFileName);

                            //使用IOUtils实现文件的copy
                            IOUtils.copy(inputStream,fileOutputStream);

                            //关闭流
                            fileOutputStream.close();
                            inputStream.close();
                        }
                    }
                }
            }
        } catch (FileUploadException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
